import { Component, OnInit } from '@angular/core';
import { WeekWorkService } from './weekwork.service';
import { LocalStorage } from '@core/local.storage';
import { ActivatedRoute } from '@angular/router';
import { _HttpClient } from '@delon/theme';
import { environment } from '../../../../environments/environment';

@Component({
  selector: 'app-weekwork',
  templateUrl: './weekwork.component.html',
  styleUrls: ['./weekwork.component.less'],
  providers: [WeekWorkService]
})
export class WeekworkComponent implements OnInit {
  lockHeadScroll = { y: window.innerHeight - 350 + 'px' };
  inputParam: any = {
    gqbz: null,
    gqsp: null,
    gqck: null,
    cjbz: null,
    cjsp: null,
    cjck: null,
    jskbz: null,
    jsksp: null,
    jskck: null
  };
  showPage1 = true;
  showPage2 = false;
  tabIndex = 0;
  page2Param: any = {};
  userInfo = JSON.parse(this.ls.get('loginInfo'));
  userType = '';
  ddType: any;

  cjList: any = [];
  showCjlist = false;
  tabCfg: any;
  bmList = [];

  constructor(
    private activatedRoute: ActivatedRoute,
    private weekWorkService: WeekWorkService,
    private ls: LocalStorage,
    private http: _HttpClient
  ) { }

  ngOnInit() {
    this.activatedRoute.params.subscribe(params => {
      if (params && params.type) {
        this.ddType = params.type;
      }
      this.initTab();
      this.initUserType();
      this.tabSelectChange({ index: 0 });
      this.getBm();
    })
  }

  initUserType() {
    switch (this.userInfo.N_LEVEL) {
      case 10:
        this.userType = 'gq';
        break;
      case 30:
        this.userType = 'cj';
        break;
      case 50:
        this.userType = this.ddType;
        this.getCj();
        break;
    }
  }

  getCj() {
    this.weekWorkService.getCjList().subscribe(res => {
      this.cjList = res;
    })
  }

  trDbclick(row) {
    const tab = row._tab;
    const add_delArr = ['gqbz', 'cjbz', 'cjsp', 'jskbz', 'jsksp', 'gqsp'];
    this.showPage(2);
    this.page2Param = row;
    const add_del = add_delArr.includes(tab);
    this.page2Param._editable = true;
    this.page2Param._deleteable = add_del;
    this.page2Param._addable = add_del;
  }

  getBm() {
    if (this.bmList.length) {
      return;
    }
    const param: any = { showSeg: 1, showAll: 1 };
    const dataurl = environment.apiBase + '/csmis/getKmnOrgTree.jwt';
    this.http.get(dataurl, param).subscribe(res => {
      this.bmList = res;
    })
  }

  showPage(num) {
    switch (num) {
      case 1:
        this.showPage1 = true;
        this.showPage2 = false;
        break;
      case 2:
        this.showPage1 = false;
        this.showPage2 = true;
        break;
    }
  }

  cjDbclick(row) {
    this.showGq(row);
  }

  showCj() {
    this.showCjlist = true;
  }
  showGq(row) {
    this.showCjlist = false;
    this.startInputQuery('gqck', { orgId: row.opValue, bmList: this.bmList });
  }

  startInputQuery(tabId, param) {
    this.inputParam[tabId] = param;
  }

  initTab() {
    this.tabCfg = {
      gq: [{ id: 'gqbz', title: '编制' }],
      cj: [{ id: 'cjbz', title: '编制' }, { id: 'gqsp', title: '审批' }],
      gcs: [{ id: 'jskbz', title: '编制' }, { id: 'cjck', title: '车间查看' }, { id: 'gqck', title: '工区查看' }],
      jskz: [{ id: 'jsksp', title: '技术科审批' }, { id: 'jskck', title: '技术科查看' }, { id: 'cjck', title: '车间查看' }, { id: 'gqck', title: '工区查看' }],
      dz: [{ id: 'jsksp', title: '技术科审批' }, { id: 'jskck', title: '技术科查看' }, { id: 'cjck', title: '车间查看' }, { id: 'gqck', title: '工区查看' }],
    }
  }

  tabSelectChange(e) {
    if (this.userType) {
      const tabId = this.tabCfg[this.userType][e.index].id;
      this.startInputQuery(tabId, {});
      this.showCjlist = tabId === 'gqck' ? true : false;
    }
  }
}
